<template>
  <div class="session-list">
    <!-- 可调整高度的分界线 -->
    <div 
      class="session-resizer" 
      @mousedown="startResize"
      :class="{ resizing: isResizing }"
    ></div>
    
    <div class="session-list-header">
      <h2>会话</h2>
      <button class="primary" @click="createSession" title="新建会话">
        <plus-icon class="icon" />
      </button>
    </div>
    
    <div class="session-items" :style="{ height: sessionItemsHeight + 'px' }">
      <div
        v-for="session in sessions"
        :key="session.session_id"
        class="session-item"
        :class="{ active: session.session_id === currentSession }"
        @click="selectSession(session.session_id)"
      >
        <div class="session-name">{{ session.session_name || session.session_id }}</div>
        <button 
          class="delete-btn" 
          @click.stop="deleteSession(session.session_id)"
          title="删除会话"
        >
          <trash-icon class="icon" />
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, defineProps, defineEmits, onMounted, onUnmounted } from 'vue';
import { PlusIcon, TrashIcon } from 'vue-tabler-icons';

const props = defineProps({
  sessions: {
    type: Array,
    required: true
  },
  currentSession: {
    type: String,
    required: true
  }
});

const emits = defineEmits(['select', 'delete', 'create']);

// 会话列表高度调整
const sessionItemsHeight = ref(300);
const isResizing = ref(false);

const selectSession = (sessionId) => {
  emits('select', sessionId);
};

const deleteSession = (sessionId) => {
  const session = props.sessions.find(s => s.session_id === sessionId);
  const displayName = session ? (session.session_name || sessionId) : sessionId;
  
  if (confirm(`确定要删除会话 "${displayName}" 吗？`)) {
    emits('delete', sessionId);
  }
};

const createSession = () => {
  // 生成唯一的会话ID（使用时间戳）
  const sessionId = `session_${Date.now()}`;
  emits('create', sessionId);
};

// 开始调整会话列表高度
const startResize = (e) => {
  isResizing.value = true;
  document.addEventListener('mousemove', handleResize);
  document.addEventListener('mouseup', stopResize);
  document.body.style.cursor = 'row-resize';
  document.body.style.userSelect = 'none';
  e.preventDefault();
};

// 处理调整高度
const handleResize = (e) => {
  if (!isResizing.value) return;
  
  const resizer = document.querySelector('.session-resizer');
  if (!resizer) return;
  
  const resizerRect = resizer.getBoundingClientRect();
  const newHeight = e.clientY - resizerRect.bottom;
  const minHeight = 100;
  const maxHeight = window.innerHeight * 0.6;
  
  
  if (newHeight >= minHeight && newHeight <= maxHeight) {
    sessionItemsHeight.value = newHeight;
  }
};

// 停止调整高度
const stopResize = () => {
  isResizing.value = false;
  document.removeEventListener('mousemove', handleResize);
  document.removeEventListener('mouseup', stopResize);
  document.body.style.cursor = '';
  document.body.style.userSelect = '';
};

// 组件挂载时初始化高度
onMounted(() => {
  // 根据窗口大小设置初始高度
  const initialHeight = Math.min(300, window.innerHeight * 0.4);
  sessionItemsHeight.value = initialHeight;
});

// 组件卸载时清理事件监听器
onUnmounted(() => {
  document.removeEventListener('mousemove', handleResize);
  document.removeEventListener('mouseup', stopResize);
});
</script>

<style scoped>
.session-list {
  display: flex;
  flex-direction: column;
  height: 100%;
  border-right: 1px solid var(--border-color);
  position: relative;
}

/* 会话列表调整器 */
.session-resizer {
  height: 4px;
  background-color: var(--border-color);
  cursor: row-resize;
  position: relative;
  transition: background-color 0.2s ease;
  margin-bottom: 0.5rem;
}

.session-resizer:hover {
  background-color: var(--primary-color);
}

.session-resizer.resizing {
  background-color: var(--primary-color);
}

.session-resizer::before {
  content: '';
  position: absolute;
  top: -2px;
  left: 0;
  right: 0;
  bottom: -2px;
  background-color: transparent;
}

.session-list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  border-bottom: 1px solid var(--border-color);
}

.session-list-header h2 {
  font-size: 1.25rem;
  font-weight: 600;
}

.icon {
  width: 1.25rem;
  height: 1.25rem;
}

.session-items {
  overflow-y: auto;
  padding: 0.5rem;
  min-height: 100px;
}

.session-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem;
  border-radius: var(--radius);
  margin-bottom: 0.5rem;
  cursor: pointer;
  transition: background-color 0.2s;
}

.session-item:hover {
  background-color: var(--bg-color);
}

.session-item.active {
  background-color: var(--primary-color);
  color: white;
}

.delete-btn {
  background: none;
  padding: 0.25rem;
  opacity: 0.7;
  display: none;
}

.session-item:hover .delete-btn {
  display: block;
}

.session-item.active .delete-btn {
  color: white;
}

.delete-btn:hover {
  opacity: 1;
  background-color: rgba(0, 0, 0, 0.1);
}

.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
}

.dialog {
  background-color: var(--card-bg);
  border-radius: var(--radius);
  padding: 1.5rem;
  width: 100%;
  max-width: 400px;
}

.dialog h3 {
  margin-bottom: 1rem;
  font-size: 1.25rem;
}

.dialog input {
  width: 100%;
  margin-bottom: 1rem;
}

.dialog-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}
</style>
